package com.suse.warmstill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.suse.warmstill.entity.TbRoleMenu;
import com.suse.warmstill.mapper.TbRoleMenuMapper;
import com.suse.warmstill.service.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author 黄靖
 * @author 2022-10-20 07:14
 * description:
 **/
@Service
public class RoleMenuServiceImpl implements RoleMenuService {
    @Autowired
    private TbRoleMenuMapper roleMenuMapper;


    @Override
    public List<Long> listMenuByRoleId(Long roleId) {
        final LambdaQueryWrapper<TbRoleMenu> queryWrapper = new LambdaQueryWrapper<TbRoleMenu>().eq(TbRoleMenu::getRoleId, roleId);
        final List<Long> menuList = roleMenuMapper.selectList(queryWrapper).stream().map(TbRoleMenu::getMenuId).collect(Collectors.toList());

        return menuList;
    }

    @Override
    public void inserRoleMenu(Long roleId, List<Long> menuList) {
        menuList.forEach((menuId) -> {
            final TbRoleMenu tbRoleMenu = new TbRoleMenu();
            tbRoleMenu.setRoleId(roleId);
            tbRoleMenu.setMenuId(menuId);
            roleMenuMapper.insert(tbRoleMenu);

        });


    }

    @Override
    public List<TbRoleMenu> listRoleByMenuId(Long menuId) {
        final List<TbRoleMenu> tbRoleMenus = roleMenuMapper.selectList(new LambdaQueryWrapper<TbRoleMenu>().eq(TbRoleMenu::getMenuId, menuId));


        return tbRoleMenus;
    }

    @Override
    public void deleteByMenuId(Long menuId) {
        final LambdaQueryWrapper<TbRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbRoleMenu::getMenuId, menuId);
        roleMenuMapper.delete(queryWrapper);
    }

    @Override
    public void deleteByRoleId(Long roleId) {
        final LambdaQueryWrapper<TbRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbRoleMenu::getRoleId, roleId);
        roleMenuMapper.delete(queryWrapper);
    }
}
